Skip to content

picogame: its types cannot be subclassed in Python - #12048

Open
lynt-smitka wants to merge 1 commit into
adafruit:mainfrom
MakerClassCZ:picogame-no-subclass
Open

lynt-smitka wants to merge 1 commit into
adafruit:mainfrom
MakerClassCZ:picogame-no-subclass

Conversation

@lynt-smitka

Copy link
Copy Markdown

Subclassing picogame types never worked: a Scene rejects subclass instances ("Invalid item"), and the native code used the subclass instance as its own struct, so properties read and wrote the wrong memory.

Making it work would cost about 800 bytes of flash for a feature nobody uses, so this makes such a class definition fail right away with TypeError instead.

That needs a small core change, a new MP_TYPE_FLAG_NOT_SUBCLASSABLE checked in type_new, because a native type's make_new is called with the native type and cannot tell that a subclass is being created.

CircuitPython passes a subclass instance as self to native methods and properties, and picogame uses self as its own struct, so a subclass of Sprite read and wrote the wrong memory. Defining such a subclass now raises TypeError, from a new MP_TYPE_FLAG_NOT_SUBCLASSABLE.
@dhalbert

dhalbert commented Dec 5, 2026

Copy link
Copy Markdown
Collaborator

This has always been true for many (but not all) natives types, and is inherited from MicroPython. I agree that it would be nice to raise an error. I can't remember the exact semantics. Discussion here: https://docs.micropython.org/en/latest/genrst/core_language.html, but I am pretty sure this has been discussed multiple times in the MicroPython issues and maybe in a few CircuitPython issues too.

@lynt-smitka

Copy link
Copy Markdown
Author

Thanks, I've been reading the discussions, and it seems that, according to MPy, the right approach is to allow subclasses. I don't think it's worth 800B, and the issue isn't that significant, so I'd probably pass this PR...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants